#include "background.h"
#include "step.h"
#include "step_definition.h"

using namespace CppStory;

Background::Background()
: cur_step(NULL)
, result(false)
{
}

Background::~Background()
{
    std::vector< Step* >::iterator step_iter = steps.begin();
    while (step_iter != steps.end())
    {
        delete *step_iter;
#ifdef WIN32
        step_iter = steps.erase(step_iter);
#else
        steps.erase(step_iter);
#endif
    }
}

void Background::register_step(Step *c)
{
    steps.push_back(c);
    cur_step = c;
}

bool Background::run()
{
    std::vector< Step* >::iterator step_iter;
    for (step_iter = steps.begin();
         step_iter != steps.end();
         ++step_iter)
    {
        if (!(*step_iter)->run())
            return false;
    }
    result = true;
    return true;
}

void Background::clean_contexts()
{
    std::vector< Step* >::iterator step_iter;
    for (step_iter = steps.begin();
         step_iter != steps.end();
         ++step_iter)
    {
        if ((*step_iter)->get_step_def())
        {
            (*step_iter)->get_step_def()->clean_context_ptrs();
        }
    }
}

